package com.ruoyi.web.web.form;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.web.core.config.AppConf;
import com.ruoyi.accountant.domain.Account;
import com.ruoyi.accountant.domain.Currency;
import com.ruoyi.accountant.domain.Reason;
import com.ruoyi.accountant.service.AccountService;
import com.ruoyi.accountant.service.CurrencyService;
import com.ruoyi.accountant.service.ReasonService;
import lombok.Data;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;

/***
 * 支出表单
 */
@Data
public class PayForm implements Validatable {
    private Long account; // 账户ID
    private String transactionType; // 交易类型
    private Integer currency; // 币种类型
    private Date payDate = new Date(); // 付款日期
    private Date proofDate = new Date(); // 凭证日期
    private Long reasonId; //原因
    private String object; //收入来源对象
    private Double money; // 收入金额
    private Double serviceCharge = 0D; // 手续费
    private String comment; //备注
    private String taxRateType; // 税率类型
    private Double taxRate; // 税率
    private String tag; // 标签
    private String attachment; //附件
    private Long ofCompany;

    private String deviceName; //设备名称
    private String deviceCode; // 设备编号
    private Double deviceMoney = 0D; // 设备总额
    private Integer deviceCount = 0; // 设备数量
    private Integer month = 0; // 耐用月数
    @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
    private Date startDate; //折旧开始月数

    private String accountName; // 账户名称
    private String reasonName; // 原因名称
    private String currencyName; //货币名称

    public Validation validate(boolean api, AccountService accountService,
                               CurrencyService currencyService,
                               ReasonService reasonService, HttpSession session) {

        if(api) {

            if(StringUtils.isEmpty(accountName)) {
                return Validation.error("账户名错误");
            } else if(StringUtils.isEmpty(currencyName)) {
                return Validation.error("币种不能为空");
            } else if(StringUtils.isEmpty(reasonName)) {
                return Validation.error("原因不能为空");
            }

            Account account = accountService.findByAname(accountName);
            Currency currency = currencyService.findByEnName(currencyName);
            Reason reason = reasonService.findByRname(reasonName);
            if(account == null) {
                return Validation.error("账户不存在");
            } else if(currency == null) {
                return Validation.error("币种不存在");
            } else if(reason == null || reason.getLevel() != 3) {
                return Validation.error("原因不存在或不是具体原因");
            }

            this.account = account.getAid();
            this.currency = currency.getCid();
            this.reasonId = reason.getRid();

            if(!reason.getRtype().equals("支出")) {
                return Validation.error("原因类型不适用");
            }

        } else {
            if(this.currency == null) {
                return Validation.error("币种不能为空");
            } else if(this.account == null) {
                return Validation.error("账户不能为空");
            } else if(this.reasonId == null) {
                return Validation.error("原因不能为空");
            }

            Currency currency = currencyService.findById(this.currency);
            Account account = accountService.findById(this.account);
            Reason reason = reasonService.findById(this.reasonId);

            if(currency == null) {
                return Validation.error("币种不存在");
            } else {
                this.currencyName = currency.getEnName();
            }

            if(account == null) {
                return Validation.error("账户不存在");
            }

            if(reason == null || reason.getLevel() != 3) {
                return Validation.error("原因不存在或不是具体原因");
            }

            if(!reason.getRtype().equals("支出")) {
                return Validation.error("原因类型不适用");
            }
        }


        if(transactionType.equals("同日支出")) { // 如果是同日收入，凭证日期和实际收款日期相同

        } else if(transactionType.equals("应付款")) { // 需要输入凭证日期，收款日期为空
            payDate = null;
            if(proofDate == null) {
                return Validation.error("应付款需要输入凭证日期");
            }
        } else if(transactionType.equals("非同日支出")) {
            if(proofDate == null || payDate == null) {
                return Validation.error("非同日支出需要输入收款日期和凭证日期");
            }
        }

        if(money == null || money <= 0) {
            return Validation.error("金额错误");
        }

        if(serviceCharge == null || serviceCharge < 0) {
            return Validation.error("手续费错误");
        }

        if(taxRateType == null || !taxRateType.equals("应税") && !taxRateType.equals("免税") && !taxRateType.equals("零税率")) {
            return Validation.error("税率类型错误");
        }

        if(taxRateType.equals("应税") && taxRate <= 0) {
            return Validation.error("应税率错误:"+taxRate);
        }

        Account byId = accountService.findById(account);
        if(byId.getStop() == AppConf.ON) {
            return Validation.error(byId.getAname()+"已经停用");
        }

        // 检查设备
        Reason r = reasonService.findById(reasonId);
        Reason p = reasonService.findById(r.getParent());
        if(p.getRname().equals("购买设备")) { // 如果是购买设备需要填写设备购买信息
            if(StringUtils.isEmpty(deviceName)) {
                return Validation.error("设备名称错误");
            } else if(StringUtils.isEmpty(deviceCode)) {
                return Validation.error("设备编号错误");
            } else if(deviceMoney <= 0) {
                return Validation.error("设备金额错误");
            } else if(deviceCount <= 0) {
                return Validation.error("设备数量错误");
            } else if(month <= 0) {
                return Validation.error("耐用月数错误");
            } else {
                if(startDate == null) {
                    return Validation.error("折旧开始日期不能空");
                }

                Calendar nowCalendar = Calendar.getInstance();
                nowCalendar.setTime(new Date());

                Calendar startCalendar = Calendar.getInstance();
                startCalendar.setTime(startDate);

                int nowYear = nowCalendar.get(Calendar.YEAR);
                int nowMonth = nowCalendar.get(Calendar.MONTH);

                int startYear = startCalendar.get(Calendar.YEAR);
                int startMonth = startCalendar.get(Calendar.MONTH);

                if(nowYear > startYear) {
                    return Validation.error("折旧日期必须大于");
                }


            }
        }

        return Validation.ok();
    }
}
